/**
 * Created by L on 2017/6/2.
 */

var url="http://47.93.233.182/";
window.onload=function (){

    //得到数据
    getAdvicelist();

    //提交信息
    document.getElementById("button321").onclick=function () {
        var name=$('input[name="name"]').val();
        var cname=$('input[name="cname"]').val();
        var tel=$('input[name="tel"]').val();
        var email=$('input[name="mail"]').val();
        var con=$("#con").val();

        $.ajax({
            url:url+"/addContact",
            type:'GET',
            dataType:'JSON',
            data:{
                contact_username:name,
                contact_userposition:cname,
                contact_usertel:tel,
                contact_useremail:email,
                contact_content:con
            },
            success:function (text) {

                if(text.msg){
                    alert("提交成功");
                    location.reload();
                }else{
                    alert("你提交的信息有误请重新提交")
                }
            },
            error:function () {

            }
        });

    }
};

//数据请求
function getAdvicelist() {

    $.ajax({
        url:url+"/getAdvicelist",
        type:'GET',
        dataType:'JSON',
        success:function (text) {
            //问答
            questionAndAnswer(text);

        },
        error:function () {

        }
    });
}

//问答
function questionAndAnswer(text) {
    var queAndAnw=document.getElementsByClassName("about-coop")[0];
    for(var i=0;i<text.data.length;i++){
        var p0=document.createElement("p");
        p0.setAttribute("class","question");
        queAndAnw.appendChild(p0);
        $(".question").eq(i).html('<span>Q:</span>'+text.data[i].advice_question);

        var p1=document.createElement("p");
        p1.setAttribute("class","answer");
        queAndAnw.appendChild(p1);
        $(".answer").eq(i).html('<span>A:</span>'+text.data[i].advice_question);
    }

}

